package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年12月11日 20:00
 */
@Slf4j
@RequestMapping("/user/addressBook")
@RestController
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * @Author: ggm
     * @date: 2024/12/11
     * 查询当前登录用户的所有地址
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> getAllAddressBook() {
        log.info("查询所有地址");
        List<AddressBook> addressBookList = addressBookService.getAllAddressBook();
        return Result.success(addressBookList);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/11
     * 添加地址簿
     */
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("添加地址簿{}", addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/12/11
     * 查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddressBook() {
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        if (addressBook == null) {
            return Result.error("用户没有默认地址");
        } else {
            return Result.success(addressBook);
        }
    }

    /**
     * @Author: ggm
     * @date: 2024/12/11
     * 修改地址
     */
    @PutMapping
    public Result editAddressBook(@RequestBody AddressBook addressBook) {
        log.info("修改地址{}", addressBook);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    /**
     * @Author: ggm
     * @date: 2024/12/11
     * 根据id删除地址
     */
    @DeleteMapping
    public Result deleteById(@PathVariable Long id) {
        log.info("删除地址{}", id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * @Author: ggm
     * @date: 2024/12/17
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id) {
        log.info("根据id查询地址:{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);

    }

    /**
     * @Author: ggm
     * @date: 2024/12/17
     * 设置默认地址
     */
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址{}", addressBook);
        //先查询有没有设置默认地址
        AddressBook defaultAddressBook = addressBookService.getDefaultAddressBook();
        if (defaultAddressBook == null) {
            //如果没有则设置当前地址为默认地址
            addressBookService.setDefaultAddress(addressBook);
        }
        //如果有设置默认地址则将其设置为非默认地址
        addressBookService.updateIsDefaultByUserId(defaultAddressBook);
        //再将当前地址设置为默认地址
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();

    }


}
